import { requestClient } from '#/api/request';

enum Api {
  knowledgeList = '/knowledge/list',
  knowledgeDelete = '/knowledge/remove',
  knowledgeSave = '/knowledge/save',
  knowledgeDetail = '/knowledge/detail',
  knowledgeFileDelete = '/knowledge/attach/remove',
  knowledgeFragmentList = '/knowledge/fragment/list',
  ragflowDocuments = '/knowledge/ragflow/documents',       // 新增 - ragflow数据集文档列表
  ragflowDownload = '/knowledge/ragflow/download',         // 新增 - ragflow文档下载
}

// 获取列表
export function knowledgeList() {
  return requestClient.get<any>(Api.knowledgeList);
}

// 删除
export function knowledgeDelete(id: any) {
  return requestClient.post<any>(`${Api.knowledgeDelete}/${id}`);
}

// 新增
export function knowledgeSave(data: any) {
  return requestClient.post<any>(Api.knowledgeSave, data);
}

// 获取附件列表
export function knowledgeDetail(id: any) {
  return requestClient.get<any>(`${Api.knowledgeDetail}/${id}`);
}

// 附件删除
export function knowledgeFileDelete(id: any) {
  return requestClient.post<any>(`${Api.knowledgeFileDelete}/${id}`);
}

// 知识片段列表（从本地数据库）
export function knowledgeFragmentList(id: any) {
  return requestClient.get<any>(`${Api.knowledgeFragmentList}/${id}`);
}

// 从RAGFlow获取知识片段
export function getRagflowFragments(docId: any, pageNum = 1, pageSize = 100) {
  return requestClient.get<any>(`/knowledge/fragment/ragflow/${docId}?pageNum=${pageNum}&pageSize=${pageSize}`);
}

// 上传文件到Ragflow
export function uploadToRagflow(kid: string, file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return requestClient.post<any>(`/knowledge/ragflow/upload/${kid}`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

// 检查Ragflow服务状态
export function checkRagflowStatus() {
  return requestClient.get<any>('/knowledge/ragflow/status');
}

// 获取所有Ragflow数据集
export function getAllRagflowDatasets() {
  return requestClient.get<any>('/knowledge/ragflow/datasets');
}

// 同步RAGFlow数据集到本地
export function syncRagflowDatasets() {
  return requestClient.post<any>('/knowledge/ragflow/sync');
}

// 列出指定ragflow数据集下所有附件
export function listRagflowDocuments(datasetId: string) {
  return requestClient.get<any>(`${Api.ragflowDocuments}/${datasetId}`);
}
// 下载ragflow文档，返回后端代理url（a标签直接用即可）
export function downloadRagflowFile(datasetId: string, docId: string) {
  return `${Api.ragflowDownload}/${datasetId}/${docId}`;
}
